package com.walkline.screen;

import com.walkline.app.RedFlowerAppConfig;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class VendorNameScreen extends PopupScreen
{
	private LabelField _vendorName;
	private LabelField _vendorID;
	
	public VendorNameScreen(String vendorName) {this(vendorName, null);}

	public VendorNameScreen(String vendorName, String vendorID)
	{
		super(new VerticalFieldManager(NO_VERTICAL_SCROLL | VERTICAL_SCROLLBAR),Field.FOCUSABLE);

		setBorder(RedFlowerAppConfig.border_Transparent);
		setBackground(RedFlowerAppConfig.bg_Transparent);

		VerticalFieldManager vfm1 = new VerticalFieldManager(NO_VERTICAL_SCROLL | USE_ALL_WIDTH);
		VerticalFieldManager vfm2 = new VerticalFieldManager(VERTICAL_SCROLL | USE_ALL_WIDTH);

		int pos = 0;
		int curPos = 0;
		String newVendorName = "";
		
		while (vendorName.indexOf("/", curPos)>=0)
		{
			pos = vendorName.indexOf("/", curPos);

			if (pos > 0)
			{
				newVendorName += "\u2022 " + vendorName.substring(curPos, pos) + "\n";
				curPos = pos+1;
			}
		}
		newVendorName += "\u2022 " + vendorName.substring(curPos, vendorName.length());

		//_vendorName=new LabelField("\u2022 " + vendorName);
		_vendorName = new LabelField(newVendorName);
		_vendorName.setPadding(5, 0,0,0);
		
		if (vendorID != null)
		{
			_vendorID = new LabelField("ID " + vendorID, USE_ALL_WIDTH);
			_vendorID.setPadding(0, 0, 3, 0);
			
			vfm1.add(_vendorID);
			vfm1.add(new SeparatorField());
		}

		vfm2.add(_vendorName);
		add(vfm1);
		add(vfm2);
	}

	protected void paintBackground(Graphics g) {}

	protected boolean keyChar(char key, int status, int time)
	{
		onClose();
		return true;
	}
	
	protected boolean navigationClick( int status, int time ) 
    {
		onClose();
        return true;    
    }
	
	protected boolean trackwheelClick( int status, int time )
    {        
		onClose();    
        return true;
    }
	
	protected boolean touchEvent(TouchEvent message)
	{
		if(message.getEvent()==TouchEvent.UNCLICK)
		{
			onClose();
			return true;	
		}
		
		return super.touchEvent(message);
	}
	
	public boolean onClose()
	{
		UiApplication.getUiApplication().popScreen(this);
		return true;	
	}
}